package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author 王加鑫
 * @Date 2021/09/09 21:28
 * @Since JDK 1.8
 */
@RestController
@RequestMapping("smallVideos")
@Slf4j
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 发布小视频
     * @return
     */
    @PostMapping
    public ResponseEntity<ErrorResult> saveVideo(@RequestParam("videoThumbnail") MultipartFile picFile,
                                                 @RequestParam("videoFile") MultipartFile videoFile) {

        try {
            Boolean result = this.videoService.saveVideo(picFile,videoFile);
            if(result){
                return ResponseEntity.ok(null);
            }
            if (!result){
                ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("该账号发布视频功能被冻结,无法发布视频").build();
                return ResponseEntity.status(500).body(errorResult);
            }
        } catch (Exception e) {
            log.error("上传小视频失败 picFile = " + picFile + ", videoFile = " + videoFile,e);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("上传小视频失败").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    /**
     * 小视频列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam("page")Integer page, @RequestParam("pagesize") Integer pageSize) {
        try {
            PageResult pageResult = this.videoService.queryVideoList(page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("查询视频列表失败",e);
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 视频点赞
     * @param videoId
     * @return
     */
    @PostMapping("{id}/like")
    public ResponseEntity<Object> likeComment(@PathVariable("id") String videoId) {
        try {
            Long likeCount = this.videoService.likeComment(videoId);
            if(ObjectUtil.isNotEmpty(likeCount)){
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            log.error("点赞失败,videoId = " + videoId,e);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("点赞失败").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    /**
     * 视频点赞
     * @param videoId
     * @return
     */
    @PostMapping("{id}/dislike")
    public ResponseEntity<Object> disLikeComment(@PathVariable("id") String videoId) {
        try {
            Long likeCount = this.videoService.disLikeComment(videoId);
            if(ObjectUtil.isNotEmpty(likeCount)){
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            log.error("取点赞失败,videoId = " + videoId,e);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("取消点赞失败").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    /**
     * 小视频评论列表
     * @return
     */
    @GetMapping("{id}/comments")
    public ResponseEntity<PageResult> queryCommentsList(@PathVariable("id") String videoId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = this.videoService.queryCommentsList(videoId,page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("查询小视频评论列表失败，videoId = " + videoId,e);
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 小视频发布评论
     * @return
     */
    @PostMapping("{id}/comments")
    public ResponseEntity<ErrorResult> saveComments(@PathVariable("id") String videoId,@RequestBody Map<String,String> parm) {
        String comment = parm.get("comment");
        try {
            Boolean result = this.videoService.saveComments(videoId,comment);
            if(result){
                return ResponseEntity.ok(null);
            }
            if (!result){
                //说明该账号发布权限被冻结
                ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("该账号发布评论功能被冻结,无法发布评论").build();
                return ResponseEntity.status(500).body(errorResult);
            }
        } catch (Exception e) {
            log.error("发布小视频评论失败",e);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("发布小视频评论失败").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    /**
     * 评论点赞
     *
     * @param videoCommentId 视频中的评论id
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String videoCommentId) {
        try {
            Long likeCount = this.videoService.likeComment(videoCommentId);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 评论取消点赞
     *
     * @param videoCommentId 视频中的评论id
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("id") String videoCommentId) {
        try {
            Long likeCount = this.videoService.disLikeComment(videoCommentId);
            if (null != likeCount) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 视频用户关注
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity<Void> saveUserFocusComments(@PathVariable("id") Long userId) {
        try {
            Boolean bool = this.videoService.followUser(userId);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消视频用户关注
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity<Void> saveUserUnFocusComments(@PathVariable("id") Long userId) {
        try {
            Boolean bool = this.videoService.disFollowUser(userId);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
